package com.tjetc.exception;

import com.tjetc.entity.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 *
 *
 * @author gaoyongxiang
 * @version v1.0
 * @date 2025/9/8
 */
//我们的异常处理类 也可以返回一个json数据
@RestControllerAdvice
//@ControllerAdvice
public class GlobalExceptionHandle {

    //@ExceptionHandler(Exception.class)
    public String handle(Exception e){
        System.out.println("处理Exception");
        System.out.println(e.getMessage());
        return "fail";
    }



    // 我们响应的数据 一定是一个对象 是我们自己封装的一个Result对象 code（属性） 实现自定义
    @ExceptionHandler(Exception.class)
    public Result handle1(Exception e){
        System.out.println("处理Exception");
        if (e instanceof ArithmeticException) {
            return new Result(400, "算术异常");
        }
        System.out.println(e.getMessage());
        return new Result(500, "系统错误");
    }

//    @ExceptionHandler(ArithmeticException.class)
    public String handleArithmeticException(Exception e){
        System.out.println("处理ArithmeticException");
        System.out.println(e.getMessage());
        return "fail";
    }




}